package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.common.GraphModel;
import ru.amse.tsyganov.jumleditor.model.common.State;
import ru.amse.tsyganov.jumleditor.view.common.GraphView;
import ru.amse.tsyganov.jumleditor.view.common.StateView;

/*package*/ class NewStateCommand extends Command {
	
	private final GraphView<? extends GraphModel> graphView;
	
	private final StateView<? extends State> stateView;
	
	public NewStateCommand(
			GraphView<? extends GraphModel> activityGraphView,
			StateView<? extends State> stateView) {
		
		if (activityGraphView == null || stateView == null) {
			throw new IllegalArgumentException();
		}
		
		this.graphView = activityGraphView;
		this.stateView = stateView;
	}
	
	@Override
	public void execute() {
		graphView.getModelViewMap().getStatesMap().
			put(stateView.getModel(), stateView);
		
		GraphModel graphModel = graphView.getModel();
		
		graphModel.addState(stateView.getModel());
	}

	@Override
	public void unexecute() {
		graphView.getModelViewMap().getStatesMap().
			remove(stateView.getModel());
		
		graphView.getSelectedItems().
			selectedStatesRemove(stateView);
		
		GraphModel graphModel = graphView.getModel();
		
		graphModel.removeState(stateView.getModel());
		graphView.getSelectedItems().
			getSelectedStates().remove(stateView);
	}
}